<template>
	<div class="ks_body ks_body_red">
		<div class="banner">
			<image :src="dateInfo.banner" mode="widthFix"></image>
		</div>
		<div class="limitBox">
			<div class="limitInfo" v-if="status == 0">
				<h4>暂无权限，请先申请权限</h4>
				<div class="inpBox">
					<div class="inp" @click="showBmlist = true">{{selectBm}}</div>
				</div>
				<div class="inpBox">
					<u--input
					    placeholder="申请人姓名"
					    border="surround"
					    v-model="sq_name"
					  ></u--input>
				</div>
				<div class="btnBox">
					<u-button type="error" text="立即申请" @click="shenqing"></u-button>
				</div>
			</div>
			<div class="limitInfo" v-if="status == 1">
				<h4>申请成功，等待审核</h4>
				<div class="textCenter">
					<u-icon name="hourglass" size="150" color="#2b85e4"></u-icon>
				</div>
				<u-button type="error" text="刷新" @click="getLimit()"></u-button>
			</div>
			<div class="limitInfo" v-if="status == 2">
				<h4>审核通过！</h4>
				<u-button type="error" text="查看投诉列表" @click="toList()"></u-button>
			</div>
		</div>
		
		<u-picker :show="showBmlist" :columns="[dateInfo.bm]" @confirm="confirm"></u-picker>
	</div>
</template>

<script>
	import { mapActions } from 'vuex';
	const db = wx.cloud.database()
export default {
	name: 'headcomd',
	data() {
		return {
			openid: "",
			banner: "https://www.baixuemianshi.com/zhuanti/images/banner_ks.jpg?" + new Date(),
			dateInfo:"",
			status: 0, //0是没申请过,1是审核中，2是已通过
			limitInfo: "",
			
			showBmlist: false,
			selectBm: "请选择申请部门",
			sq_name: ""
		};
	},
	
	onLoad(options) {
		
	},
	onShareAppMessage(options) {},
	onShareTimeline(res) {},
	computed: {
		userphone() {
			return this.$store.getters.userphone ? this.$store.getters.userphone : '';
		},
	},
	mounted() {
		let that = this
		
	},
	
	created() {
		let that = this
		that.getopenid()
		that.getBms()
	},
	methods: {
		...mapActions(['LogSetPhone']),
		getopenid() {
			let that = this;
			wx.cloud.callFunction({
				name: 'getopenid',
				data: {},
				complete: res => {
					that.openid = res.result.openid
					that.getLimit()
				}
			});
		},
		confirm(e){
			this.selectBm = e.value[0]
			this.showBmlist = false
		},
		shenqing(){
			let that = this
			if(that.selectBm == '请选择申请部门'){
				uni.showToast({
					title:"请选择申请部门",
					icon:"error"
				})
				return
			}
			if(!that.sq_name){
				uni.showToast({
					title:"请输入申请人姓名",
					icon:"error"
				})
				return
			}
			db.collection('kesu_login2').add({
				data: {
					sq_name: that.sq_name,
					bumen: that.selectBm,
					time: that.getTime(),
					status: 1
				},
				success: res =>{
					uni.showToast({
						title:'申请成功！',
						icon: "success"
					})
					that.getLimit()
				}
			})
			
		},
		getTime(){
			let that = this
			let day = new Date()
			let y = day.getFullYear()
			let m = (day.getMonth() + 1).toString().padStart(2, '0');
			let d = day.getDate().toString().padStart(2, '0');
			let hour = day.getHours().toString().padStart(2, '0');
			let miu = day.getMinutes().toString().padStart(2, '0');
			let time = y + '-' + m + '-' + d + ' ' + hour + ':' + miu
			return time
		},
		async getLimit(){
			let that = this
			let user = await db.collection('kesu_login2').where({_openid: that.openid}).get()
			console.log(user)
			if(user.data.length == 0){
				that.status = 0
			}else if(user.data[0].status == 1){
				that.status = 1
			}else{
				that.status = 2
				that.limitInfo = user.data[0]
				that.toList()
			}
		},
		toList(){
			uni.navigateTo({
				url: '../../webview/index?href=' + encodeURIComponent('https://www.baixuemianshi.com/zhuanti/kesu/?bumen=' + this.limitInfo.bumen)
			})
		},
		getBms() {
			let that = this
			uni.request({
				url: 'https://www.baixuemianshi.com/zhuanti/kesu/js/index.js',
				method: 'get',
				dataType: 'text',
				data: {
					sstimes: new Date()
				},
				success: function(res) {
					var resjson = JSON.parse(res.data);
					that.dateInfo = resjson
				}
			});
		},
	},
	components: {}
};
</script>

<style>
@import url("./index.css");

</style>

